import { ValueAnimation } from "@/base/ValueAnimation";
import { ChessPiece, ChessTeam, labels } from "@/third/chinese-chess/src/demo/chess";
import { ChessNode } from "./ChessNode";
export enum PieceNodeState {
  Normal = 0,
  Hover = 1,
  Picked = 2,
  Dead = 3
}
export class PieceNode {
  z = 0;
  moving = false;
  dead = false;
  readonly piece: ChessPiece;
  readonly parent: ChessNode;
  private _state: PieceNodeState = PieceNodeState.Normal;
  private _anim_scale = new ValueAnimation();
  private _anim_opacity = new ValueAnimation();
  private _anim_x = new ValueAnimation();
  private _anim_y = new ValueAnimation();

  constructor(parent: ChessNode, piece: ChessPiece) {
    this.parent = parent;
    this.piece = piece;
  }
  render(delta: number, time: number) {
    const { fg_ctx, board, cell_size } = this.parent;
    if (!fg_ctx || !board)
      return;
    const { piece } = this;
    const { team, type, x, y } = this.piece;
    if (team === void 0 || type === void 0)
      return;
    const [cx, cy] = this.parent.get_c_xy(x, y);
    if (x >= 0 && y >= 0 && (this._anim_x.to !== cx || this._anim_y.to !== cy)) {
      if (this.piece.first_moved) {
        this._anim_x.start(time, this._anim_x.value(time), cx);
        this._anim_y.start(time, this._anim_y.value(time), cy);
      } else {
        this._anim_x.start(time, cx, cx);
        this._anim_y.start(time, cy, cy);
      }
    }
    const _cx = this._anim_x.update(time);
    const _cy = this._anim_y.update(time);
    let next_state: PieceNodeState = PieceNodeState.Normal;
    if (x < 0 || y < 0) {
      next_state = PieceNodeState.Dead;
    } else if (piece === this.parent.picked) {
      next_state = PieceNodeState.Picked;
    } else if (piece === this.parent.hover) {
      next_state = PieceNodeState.Hover;
    }
    if (this._state !== next_state) {
      switch (next_state) {
        case PieceNodeState.Normal:
          this._anim_scale.start(time, this._anim_scale.value(time), 1);
          break;
        case PieceNodeState.Hover:
          this._anim_scale.start(time, this._anim_scale.value(time), 1.1);
          break;
        case PieceNodeState.Picked:
          this._anim_scale.start(time, this._anim_scale.value(time), 1.2);
          break;
        case PieceNodeState.Dead:
          this._anim_opacity.start(time, this._anim_opacity.value(time), 0);
          this._anim_scale.start(time, this._anim_scale.value(time), 2);
          break;
      }
      this._state = next_state;
    }
    const scale = this._anim_scale.update(time);

    fg_ctx.save();
    fg_ctx.textAlign = 'center';
    fg_ctx.textBaseline = 'middle';

    fg_ctx.font = team === ChessTeam.Wht ? `normal ${Math.round(cell_size)}px serif` : `normal ${Math.round(cell_size)}px serif`

    fg_ctx.globalAlpha = this._anim_opacity.update(time);
    fg_ctx.translate((1 - scale) * _cx, (1 - scale) * _cy);
    fg_ctx.scale(scale, scale);

    fg_ctx.fillStyle = team === ChessTeam.Wht ? '#ffffff' : '#000000';
    fg_ctx.fillText(labels[team][type], _cx, _cy);
    fg_ctx.restore();

    this.moving = (!this._anim_x.is_end || !this._anim_y.is_end);
    this.dead = this._state === PieceNodeState.Dead && this._anim_scale.is_end && this._anim_opacity.is_end;
    if (this.moving) this.z = 2;
    if (this.dead) this.z = 0;
  }
}

